package com.santiao.publics.ad.ads;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationRewardManager;
import com.santiao.publics.config.Const;
import com.umeng.umcrash.UMCrash;



/**
 *
 * 融合demo，【激励视频广】告使用示例。更多功能参考接入文档。
 *
 * 注意：每次加载的广告，只能展示一次
 *
 * 接入步骤：
 * 1、创建AdSlot对象
 * 2、创建TTAdNative对象
 * 3、创建加载、展示监听器
 * 4、加载广告
 * 5、加载成功后，展示广告
 * 6、在onDestroy中销毁广告
 */
public class MediationRewardService     {



    //激励视频是否显示标志
    public    boolean ifRewardAdShow = false;
    public    boolean onRewardVideoCached = false;
    public    boolean ifShowRewardVideoAd = false;
    //是否关闭AD
    public    boolean ifRewardAdClose = false;
    public   String mMediaId; // 融合广告位
    public  TTRewardVideoAd mTTRewardVideoAd; // 插全屏广告对象
    private  TTAdNative.RewardVideoAdListener mRewardVideoListener; // 广告加载监听器
    private  TTRewardVideoAd.RewardAdInteractionListener mRewardVideoAdInteractionListener; // 广告展示监听器

    public  MediationRewardManager manager;

    public  String ecpm = "";
    public  String slotId = "";

    public  String adStyle = "3";

    public  int showRewardVideoAdCount=0;


    public     void  intiDate(Context context ,   String mMediaIds){
        showRewardVideoAdCount = 0;
        String simpleName = "";
        if(context!=null && context.getClass()!=null){
            simpleName = context.getClass().getSimpleName();
        }
        Log.d(Const.TAG,"MediationRewardService-----reward-------intiDate:"+simpleName+"----------"+showRewardVideoAdCount);
        ifRewardAdShow = false;
        ifRewardAdClose = false;
        ifShowRewardVideoAd =false;
        onRewardVideoCached =false;
         mMediaId  = mMediaIds;
//         adRequestService = new AdRequestService();
        loadRewardVideoAd(context);
    }

    public  MediationRewardManager getManager(){

        return  manager;
    }

//
//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.mediation_activity_reward);
//
//        // 聚合广告位（在GroMore平台的广告位，注意不是adn的代码位）
//        this.mMediaId = getResources().getString(R.string.reward_media_id);
//        TextView tvMediationId = findViewById(R.id.tv_media_id);
//        tvMediationId.setText(String.format(getResources().getString(R.string.ad_mediation_id), mMediaId));
//
//        // 广告加载
//        findViewById(R.id.bt_load).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                loadRewardVideoAd();
//            }
//        });
//
//        // 广告展示
//        findViewById(R.id.bt_show).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                showRewardVideoAd();
//            }
//        });
//    }

     public     void loadRewardVideoAd(Context context ) {
        try {
            /** 1、创建AdSlot对象 */
            AdSlot adslot = new AdSlot.Builder()
                    .setCodeId(mMediaId)
                    .setOrientation(TTAdConstant.ORIENTATION_VERTICAL)
                    .build();

            /** 2、创建TTAdNative对象 */
            TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(context);

            /** 3、创建加载、展示监听器 */
            initListeners();

            /** 4、加载广告 */
            adNativeLoader.loadRewardVideoAd(adslot, mRewardVideoListener);
        }catch (Exception e){
            e.printStackTrace();
            if(Const.IF_UM) {
                UMCrash.generateCustomLog(e, "MediationRewardManager");
            }
        }finally {
            AdRequestService adRequestService = new AdRequestService();
            adRequestService.initAdRequest("2",adStyle,ecpm,mMediaId,slotId,null);
            adRequestService.reportRequest();
        }

    }

    // 广告加载成功后，开始展示广告
    public   void showRewardVideoAd( Activity  activity) {

        ++showRewardVideoAdCount;
        ifShowRewardVideoAd = true;

        Log.d(Const.TAG,"MediationFeedService-----showRewardVideoAd-------intiDate:"+activity.getClass().getSimpleName()+"------------"+showRewardVideoAdCount);
        if (mTTRewardVideoAd == null) {
            Log.i(Const.TAG, "请先加载广告或等待广告加载完毕后再调用show方法");
            return;
        }
        /** 5、设置展示监听器，展示广告 */
        mTTRewardVideoAd.setRewardAdInteractionListener(mRewardVideoAdInteractionListener);
        mTTRewardVideoAd.showRewardVideoAd(activity);

    }

    public   void initListeners() {
        // 广告加载监听器
         mRewardVideoListener = new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int i, String s) {
                Log.i(Const.TAG, "reward load fail: errCode: " + i + ", errMsg: " + s);
            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                Log.i(Const.TAG, "reward load success");
                mTTRewardVideoAd = ttRewardVideoAd;
                manager = mTTRewardVideoAd.getMediationManager();
            }

            @Override
            public void onRewardVideoCached() {
                Log.i(Const.TAG, "reward cached success");
            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                Log.i(Const.TAG, "reward cached success 2");
                mTTRewardVideoAd = ttRewardVideoAd;
                onRewardVideoCached = true;
            }
        };
        // 广告展示监听器
         mRewardVideoAdInteractionListener = new TTRewardVideoAd.RewardAdInteractionListener() {
            @Override
            public void onAdShow() {
                ifRewardAdShow = true;
                Log.i(Const.TAG, "reward show");

                if(manager!=null){
                    ecpm = manager.getShowEcpm().getEcpm();
                    slotId = manager.getShowEcpm().getSlotId();
                }
                AdRequestService adRequestService = new AdRequestService();
                adRequestService. initAdRequest("0",adStyle,ecpm,mMediaId,slotId,null);
                adRequestService.reportRequest();
            }

            @Override
            public void onAdVideoBarClick() {

                Log.i(Const.TAG, "reward click");


                if(manager!=null){
                    ecpm = manager.getShowEcpm().getEcpm();
                }
                AdRequestService adRequestService = new AdRequestService();
                adRequestService.initAdRequest("1",adStyle,ecpm,mMediaId,slotId,null);
                adRequestService.reportRequest();
            }

            @Override
            public void onAdClose() {
                ifRewardAdShow = false;
                ifRewardAdClose  = true;
                Log.i(Const.TAG, "reward close");
            }

            @Override
            public void onVideoComplete() {
                Log.i(Const.TAG, "reward onVideoComplete");
            }

            @Override
            public void onVideoError() {
                Log.i(Const.TAG, "reward onVideoError");
            }

            @Override
            public void onRewardVerify(boolean b, int i, String s, int i1, String s1) {
                Log.i(Const.TAG, "reward onRewardVerify");
            }

            @Override
            public void onRewardArrived(boolean isRewardValid, int rewardType, Bundle extraInfo) {
                Log.i(Const.TAG, "reward onRewardArrived");
            }

            @Override
            public void onSkippedVideo() {
                Log.i(Const.TAG, "reward onSkippedVideo");
            }
        };
    }

//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        /** 6、在onDestroy中销毁广告 */
//        if (mTTRewardVideoAd != null && mTTRewardVideoAd.getMediationManager() != null) {
//            mTTRewardVideoAd.getMediationManager().destroy();
//        }
//    }
}
